/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package indexer;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.ArrayList;
import java.util.HashSet;

/**
 *
 * @author student
 */
 class Parser {

    String filename;
    int fileId;
    String indexFile;
    FileWriter fstream;
    BufferedWriter file;
    BufferedReader input;
    BufferedReader inputExcludeWords;
    DataOutputStream output;
    ArrayList<String> allWords;
    ArrayList<String> excludeWords;
    String[][] sinonims;
    FileReader reader;
    FileReader readerExcludeWords;

    public Parser(String _filename, String _indexFile, int _fileId) {
        filename = _filename;
        indexFile = _indexFile;
        fileId = _fileId;
        SetExcludeWord();
        try {
            reader = new FileReader(filename);
            input = new BufferedReader(reader);
            fstream = new FileWriter(indexFile);
            file = new BufferedWriter(fstream);

        } catch (Exception ex) {
            Logger.getLogger(Parser.class.getName()).log(Level.SEVERE, null, ex);
        }


    }

    void ParseFile() {
        String line;
        String[] arr = null;
        ArrayList<String> words = new ArrayList<String>();
        allWords = new ArrayList<String>();
        try {

            line = input.readLine();
            System.out.println(line);
            while (line != null) {
               // System.out.println("in while");
                words = ProcessLine(line);
                if (words.size() > 0) {
                    for (int i = 0; i < words.size(); i++) {
                        allWords.add(words.get(i).trim().toLowerCase());
                    }
                }
                line = input.readLine();

            }
            HashSet<String> aux = new HashSet<String>(allWords);
            allWords = new ArrayList<String>();
            allWords.addAll(aux);
          //  file.append("doc" + fileId + " : ");
            for (int i = 0; i < aux.size(); i++) {
                file.append(allWords.get(i) + " , ");
            }
            file.close();
            input.close();
        } catch (IOException ex) {
            Logger.getLogger(Parser.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    ArrayList<String> ProcessLine(String line) {
        ArrayList<String> words = new ArrayList<String>();
        String[] words_ = line.split(" ");
        for (int i = 0; i < words_.length; i++) {
           if( ! ContainsNumber( words_[i] ) )
            words.add(words_[i].toLowerCase());
        }
        for (int i = 0; i < words.size(); i++) {
            if (words.get(i).length() < 3) {
                words.remove(i);
            } else {
                if (words.get(i).contains(":")) {
                    String[] _wordS = words.get(i).split(":");
                    for (int j = 0; j < _wordS.length; j++) {
                        if (_wordS[j].length() > 2) {
                            words.add(_wordS[j].toLowerCase());
                        }
                    }
                    words.remove(i);
                } else {
                    if (words.get(i).contains("\\.")) {
                        String[] _wordS = words.get(i).split("\\.");
                        for (int j = 0; j < _wordS.length; j++) {
                            if (_wordS[j].length() > 2) {
                                words.add(_wordS[j].toLowerCase());
                            }
                        }
                        words.remove(i);
                    } else {
                        if (words.get(i).contains(",")) {
                            String[] _wordS = words.get(i).split(",");
                            for (int j = 0; j < _wordS.length; j++) {
                                if (_wordS[j].length() > 2) {
                                    words.add(_wordS[j].toLowerCase());
                                }
                            }
                            words.remove(i);
                        }
                    }
                }
            }

        }
        for (int i = 0; i < excludeWords.size(); i++) {
            if (words.contains(excludeWords.get(i))) {
                while (words.indexOf(excludeWords.get(i)) > -1) {
                    words.remove(words.indexOf(excludeWords.get(i)));
                }
            }

        }
 //       System.out.println(" returned " + words.size() + "this time");
        return words;

    }
    boolean ContainsNumber(String word)
    {
        boolean ret_val= false;
        for ( int i = 0 ; i < word.length();i++)
        {
            Character c = word.charAt(i);
            if( Character.isDigit(c))
            {
                ret_val =true;
                return true;
            }

        }
        if(  word.contains("#") || word.contains("/") || word.contains("}") || word.contains("{") )
        ret_val = true;
            return ret_val;
    }

    void SetExcludeWord() {
        excludeWords = new ArrayList<String>();
        excludeWords.add("a");
        excludeWords.add("the");
        excludeWords.add("and");
        excludeWords.add("of");
        excludeWords.add("then");
        excludeWords.add("there");
        excludeWords.add("fo");
    }

   public void GetExcludeWordsFromFile(String file) {
        try {
            readerExcludeWords = new FileReader(file);
            inputExcludeWords = new BufferedReader(readerExcludeWords);
            String line = inputExcludeWords.readLine();
            System.out.println(line);
            while (line != null) {
              //  System.out.println("in exclude while");
                String[] words_ = line.split(",");
                for (int i = 0; i < words_.length; i++) {
                    excludeWords.add(words_[i].trim().toLowerCase());
                }
                line = inputExcludeWords.readLine();
            }
        } catch (Exception ex) {
            Logger.getLogger(Parser.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
